﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace KCL.Code.Data
{
    public class SizeDAO : BaseDAO
    {
        public List<Size> GetAllSizes()
        {
            try
            {
                KCLDataContext ctx = new KCLDataContext();
                var lstSizes = from s in ctx.Sizes
                               where s.isActive == true
                               orderby s.length, s.width
                               select s;
                return lstSizes.AsEnumerable().ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public bool Insert(Size s)
        {
            try
            {
                KCLDataContext ctx = new KCLDataContext();
                ctx.Sizes.InsertOnSubmit(s);
                ctx.SubmitChanges();
                return true;
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                if (ex.Number == 2627)
                {
                    try
                    {
                        KCLDataContext ctx = new KCLDataContext();
                        var sizeToRestore = (from si in ctx.Sizes
                                             where si.length.ToUpper() == s.length.ToUpper()
                                             && si.width.ToUpper() == s.width.ToUpper()
                                             && si.isActive == false
                                             select si).FirstOrDefault();
                        sizeToRestore.length = s.length;
                        sizeToRestore.width = s.width;
                        sizeToRestore.isActive = true;
                        ctx.SubmitChanges();
                        return true;
                    }
                    catch (Exception)
                    {
                        return false;
                    }
                }
                return false;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Update(Size s)
        {
            try
            {
                KCLDataContext ctx = new KCLDataContext();
                var sizeToUpdate = (from si in ctx.Sizes
                                    where si.id == s.id
                                    select si).FirstOrDefault();
                sizeToUpdate.length = s.length.Trim();
                sizeToUpdate.width = s.width.Trim();
                sizeToUpdate.isActive = s.isActive;
                if (sizeToUpdate.length.Length == 0 ||
                    sizeToUpdate.width.Length == 0)
                    return false;
                ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Delete(Size s)
        {
            try
            {
                KCLDataContext ctx = new KCLDataContext();
                var sizeToDelete = (from si in ctx.Sizes
                                    where si.id == s.id
                                    select si).FirstOrDefault();
                sizeToDelete.isActive = false;
                ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}